import numpy as np
import math


def arrange_schedule(order, x, y):
    if order > 1:
        for i in range(order):
            # 此算法只适用于2的k次方的情形，若矩阵阶数开方后为无理数，则不适用
            if schedule_table[x + i, y] == 0:
                schedule_table[x + i, y] = x + y + 1 + i
                if schedule_table[x + i, y] > size:
                    schedule_table[x + i, y] -= size
            if schedule_table[x + i, y + i] == 0:
                schedule_table[x + i, y + i] = x + y + 1
                if schedule_table[x + i, y + i] > size:
                    schedule_table[x + i, y + i] -= size
            if schedule_table[x, y + i] == 0:
                schedule_table[x, y + i] = x + y + 1 + i
                if schedule_table[x, y + i] > size:
                    schedule_table[x, y + i] -= size
            # if i != 0 and x >= order and y >= order and schedule_table[x + i][y] > size \
            #         and schedule_table[x][y + i] > size:
            #     schedule_table[x + i, y] = schedule_table[x + i - order][y - order]
            #     schedule_table[x, y + i] = schedule_table[x - order][y + i - order]
            #     schedule_table[x + i, y + i] = schedule_table[x, y]
        temp_order = math.floor(order >> 1)
        arrange_schedule(temp_order, x, y)
        arrange_schedule(temp_order, x + temp_order, y)
        arrange_schedule(temp_order, x, y + temp_order)
        arrange_schedule(temp_order, x + temp_order, y + temp_order)


if __name__ == '__main__':
    while True:
        size = int(input("请输入参赛的选手人数(必须为偶数):"))
        if size % 2 != 0:
            continue
        else:
            break
    schedule_table = np.zeros((size, size))
    print(schedule_table)
    arrange_schedule(size, 0, 0)
    print(schedule_table)
